<?php
namespace App\Http\Controllers\Admin;
use App\http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class LinkController extends BaseController{
    public function index(){
        return view('admin/index');
        //视图文件是 resources/views/admin/index.blade.php
    }
    public function toget_link_count(){
        $count = DB::table('link')->count();
        echo $count;die;
    }
    public function toget_link_list(Request $request){
        $link_p= DB::table('link')
            ->orderBy("link_addtime","DESC");
        //echo $request->admin_name;die;
        if($request->link_name){
            $link_p = $link_p->where('link_name','like','%'.$request->link_name.'%');
        }
        if($request->start){
            $link_p = $link_p->where('link_addtime','>',strtotime($request->start));
        }
        if($request->end){
            $link_p = $link_p->where('link_addtime','<',strtotime($request->end));
        }
        $link_p = $link_p->paginate(5);
        $link = $link_p->items();
        $current = $link_p->currentPage();
        $a = ($current-1)*5+1;
        echo '
        <table class="layui-table">
            <thead>
                <tr>
                    <th>编号</th>
                    <th>链接名</th>
                    <th>链接地址</th>
                    <th>链接图片</th>
                    <th>添加时间</th>
                    <th >操作</th>
                </tr>
            </thead>
            <tbody>
        ';
//        echo '<pre>';
//         print_r($link);
//        echo '</pre>';
        foreach ($link as $value){
            $link_time = date('Y-m-d',$value->link_addtime);
            //echo $link_time;die;
            echo '
            <tr>
                <td>'.$a++.'</td>
                <td>'.$value->link_name.'</td>
                <td>'.$value->link_url.'</td>
                <td>'.$value->link_img.'</td>
                <td>'.$link_time.'</td>
                <td class="td-manage">
                    <a title="查看"  onclick="x_admin_show('."'编辑'".','."'/admin/link_edit/$value->link_id'".')" href="javascript:;">
                        <i class="layui-icon">&#xe63c;</i>
                    </a>
                    <a title="删除" onclick="member_del(this,'.$value->link_id.')" href="javascript:;">
                        <i class="layui-icon">&#xe640;</i>
                    </a>
                </td>
            </tr>
            ';

        }
        echo '
            </tbody>
            </table>
            <div class="page">'.$link_p->render().'
            </div>
        ';
    }
    public function link_list(){

        // echo 13213;
        //show_msg('3213','/');
        return view('admin/link/link_list');
    }
    public function link_add(){

         //echo 13213;
//        show_msg('3213','/');
        return view('admin/link/link_add');
    }
    public function toget_admin_count(Request $request){
        $count = DB::table('link');
        if($request->admin_name){
            $count = $count->where('link_name','like','%'.$request->link_name.'%');
        }
        if($request->start){
            $count = $count->where('link_addtime','>',strtotime($request->start));
        }
        if($request->end){
            $count = $count->where('link_addtime','<',strtotime($request->end));
        }
        $count = $count->count();
        echo $count;die;
    }
    public function todo_addlink(Request $request){
        $data = array(
            "link_name"=>$request->link_name,
            "link_url"=>$request->link_url,
            "link_sort"=>$request->link_sort,
            "link_addtime"=>strtotime($request->link_addtime)
        );
        if($request->hasFile('file')&&$request->file('file')->isValid()){
            $file=$request->file('file');
            $allowed_extensions = ["png", "jpg", "gif"];
            if (!in_array($file->getClientOriginalExtension(), $allowed_extensions)) {
                echo json_encode(array(
                    "code"=>"2",
                    "msg"=>"只能上传png,jpg和gif格式的图片"
                ));die;
            }else{
                $destinationPath = 'uploads/link/'; //public 文件夹下面建 uploads 文件夹
                $extension = $file->getClientOriginalExtension();
                $fileName=md5(time().rand(1,1000)).'.'.$extension;
                $file->move($destinationPath,$fileName);
                $data["link_img"]="link/".$fileName;

            }
        }else{
            echo json_encode(array(
                "code"=>"2",
                "msg"=>"图片上传失败请重试"
            ));die;
        }
        $res = DB::table("link")->insert($data);
        if($res){
            echo json_encode(array(
                "code"=>"1",
                "msg"=>"添加成功"
            ));die;
        }else{
            echo json_encode(array(
                "code"=>"2",
                "msg"=>"数据执行有误"
            ));die;
        }


    }
    public function link_edit($link){
        $link = DB::table('link')->where('link_id',$link)->first();
        $link->link_addtime = date('Y-m-d',$link->link_addtime);
        return view('admin/link/link_edit',['link'=>$link]);
    }
    public function togo_link_edit(Request $request){

        $data = array(
            "link_name"=>$request->link_name,
            "link_url"=>$request->link_url,
            "link_addtime"=>strtotime($request->link_addtime)
        );
        if($request->hasFile('file')&&$request->file('file')->isValid()){
            $file=$request->file('file');
            $allowed_extensions = ["png", "jpg", "gif"];
            if (!in_array($file->getClientOriginalExtension(), $allowed_extensions)) {
                echo json_encode(array(
                    "code"=>"2",
                    "msg"=>"只能上传png,jpg和gif格式的图片"
                ));die;
            }else{
                //删除旧图片
                $old=DB::table('link')->select('link_img')->where("link_id",$request->link_id)->first();
                $link_img=$old->link_img;//文件名
                $path='./uploads/'.$link_img;//完整路径
                if(!empty($link_img) && file_exists($path)){
                    unlink($path);//当图片字段不为空并且有该图片文件的时候就删除
                }
                $destinationPath = 'uploads/link/'; //public 文件夹下面建 uploads 文件夹
                $extension = $file->getClientOriginalExtension();
                $fileName=md5(time().rand(1,1000)).'.'.$extension;
                $file->move($destinationPath,$fileName);
                $data["link_img"]="link/".$fileName;

            }
        }
        $res = DB::table("link")->where('link_id',$request->link_id)->update($data);
        if($res){
            echo json_encode(array(
                "code"=>"1",
                "msg"=>"编辑成功"
            ));die;
        }else{
            echo json_encode(array(
                "code"=>"2",
                "msg"=>"数据执行有误"
            ));die;
        }
    }
    public function todo_link_del(Request $request){
        $islogin = $this->islogin();
        if (!$islogin) {
            return redirect("/admin/login");
        }
        $link_id = $request->link_id;
        $link = DB::table("link")->where("link_id",$link_id)->first();
        $img_path = $link->link_img;
        if ($img_path&&file_exists("./uploads/".$img_path)) {
            unlink("./uploads/".$img_path);
        }
        $res = DB::table("link")->where("link_id",$link_id)->delete();
        if ($res) {
            echo json_encode(array(
                "code"=>"1",
                "msg"=>"删除成功"
            ));die;
        }else{
            echo json_encode(array(
                "code"=>"2",
                "msg"=>"数据库执行有误"
            ));die;
        }
    }
}